package view;

import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

import model.Usuario;
import model.dao.LogIO;
import control.ControllersDaos;
import control.ThreadsClass;

public class VLogin extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField txtUsurio;
	private JPasswordField pwdSenha;
	private JLabel label_Result;
	private ControllersDaos controller; 
	LogIO log;
	ThreadsClass t1;
	ThreadsClass t2;
	/**
	 * Launch the application.
	 */
	/*
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					VLogin frame = new VLogin();
					frame.setVisible(true);
					frame.setResizable(false);
					frame.setLocationRelativeTo(null);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
*/
	/**
	 * Create the frame.
	 */
	public VLogin(int Repositorio){
		this();
		this.setLocationRelativeTo(null);
		controller = ControllersDaos.getInstance(Repositorio);
		
	}
	
	private  VLogin () {
		setTitle("Autentica\u00E7\u00E3o Controle de Chamados Help-Desk");
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBackground(Color.WHITE);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		txtUsurio = new JTextField();
		txtUsurio.setBorder(new LineBorder(new Color(0, 0, 0)));
		txtUsurio.setBackground(Color.LIGHT_GRAY);
		txtUsurio.setHorizontalAlignment(SwingConstants.CENTER);
		txtUsurio.setFont(new Font("Times New Roman", Font.BOLD, 16));
		txtUsurio.setText("admin");
		txtUsurio.setBounds(148, 83, 128, 29);
		contentPane.add(txtUsurio);
		txtUsurio.setColumns(10);
		txtUsurio.setSelectionStart(0); // deixar todo texto selecionado
		
		pwdSenha = new JPasswordField();
		pwdSenha.setBorder(new LineBorder(new Color(0, 0, 0)));
		pwdSenha.setBackground(Color.LIGHT_GRAY);
		pwdSenha.setHorizontalAlignment(SwingConstants.CENTER);
		pwdSenha.setText(" ");
		pwdSenha.setFont(new Font("Times New Roman", Font.BOLD, 16));
		pwdSenha.setEchoChar('*');
		pwdSenha.setToolTipText("");
		pwdSenha.setBounds(148, 135, 128, 29);
		pwdSenha.setSelectionStart(0);
		contentPane.add(pwdSenha);
		
		label_Result = new JLabel("");
		label_Result.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
		label_Result.setHorizontalTextPosition(SwingConstants.CENTER);
		label_Result.setHorizontalAlignment(SwingConstants.CENTER);
		label_Result.setForeground(Color.RED);
		label_Result.setFont(new Font("Trebuchet MS", Font.BOLD | Font.ITALIC, 16));
		label_Result.setBackground(Color.WHITE);
		label_Result.setBounds(98, 175, 237, 61);
		contentPane.add(label_Result);
		
		JLabel lblEntreComO = new JLabel("Entre com o seu usu\u00E1rio e senha");
		lblEntreComO.setIcon(new ImageIcon(VLogin.class.getResource("/imagens/autentica\u00E7\u00E3o.jpg")));
		lblEntreComO.setHorizontalAlignment(SwingConstants.CENTER);
		lblEntreComO.setHorizontalTextPosition(SwingConstants.RIGHT);
		lblEntreComO.setFont(new Font("Trebuchet MS", Font.BOLD | Font.ITALIC, 14));
		lblEntreComO.setBounds(41, 11, 350, 61);
		contentPane.add(lblEntreComO);
		
		JButton btnConfimar = new JButton("Confimar");
		btnConfimar.addActionListener(new ActionListener() {
			public void actionPerformed (ActionEvent arg0) {
				String login = txtUsurio.getText();
				String senha = new String(pwdSenha.getPassword());
				
				if (login == null || login.equals("") || senha == null
						|| senha.equals("")) {
					label_Result.setText("Verifique os campos");
				} else {
					ArrayList<Usuario> listaUsuario = null;
					try {
						//listaUsuario = new UsuarioCRUD().listarUsuario();
						listaUsuario =controller.listarUsuario();       
						for (Usuario usuario : listaUsuario) {
							if(usuario.validaUsuario(login, senha)){
								VLogin.this.setVisible(false);
								VPrincipal telaPrincipal = new VPrincipal(usuario,controller);
								telaPrincipal.setVisible(true);
								 
								//log=new LogIO();
								//log.gravarLogin(login); //Grava no arquivo
								t1 = new ThreadsClass(login);
								t2 = new ThreadsClass(login);
								t1.start();
								System.out.println(t1.getState());
								t2.start();
								System.out.println(t2.getState());
							}
							
						}
						label_Result.setText("Usuário não encontrado!!");

					} catch (ClassNotFoundException e) {
						label_Result.setText("Problema interno: "+e.getMessage());
						
					} catch (NullPointerException e) {
						label_Result.setText("Problema interno: "+e.getMessage());
						e.printStackTrace();
					} catch (SQLException e) {
						label_Result.setText("Problema interno: "+e.getMessage());
						e.printStackTrace();
					}
					
				}
			}
		});
		btnConfimar.setFont(new Font("Tahoma", Font.BOLD, 11));
		btnConfimar.setMnemonic('C');
		btnConfimar.setBounds(305, 213, 108, 23);
		contentPane.add(btnConfimar);
		
		JButton btnCancelar = new JButton("Cancelar");
		btnCancelar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				VLogin.this.dispose();
				System.exit(0);
			}
		});
		btnCancelar.setFont(new Font("Tahoma", Font.BOLD, 11));
		btnCancelar.setMnemonic('r');
		btnCancelar.setBounds(24, 213, 108, 23);
		contentPane.add(btnCancelar);
	}
}
